package com.xinqi.modules.prepare.prepare.controller.web;

import com.xinqi.common.security.utils.UserHelp;
import com.xinqi.common.base.current.Currents;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.R;
import com.xinqi.modules.prepare.prepare.dto.req.PrepareCreateDTO;
import com.xinqi.modules.prepare.prepare.dto.req.PrepareQueryDTO;
import com.xinqi.modules.prepare.prepare.dto.req.PrepareUpdateDTO;
import com.xinqi.modules.prepare.prepare.dto.rsp.PrepareResultDTO;
import com.xinqi.modules.prepare.prepare.service.PrepareService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 备课区APP控制层
 *
 * @author: system
 * @date: 2023/03/19
 */
@Tag(name = "Web-备课区", description = "备课区日志Web接口")
@RestController
@RequestMapping("/web/prepare")
@RequiredArgsConstructor
public class PrepareWebController {
    private final PrepareService prepareService;

    /**
     * 备课区-分页
     */
    @Operation(summary = "备课区-分页")
    @GetMapping("/page")
    public R<PageInfo<PrepareResultDTO>> page(@ParameterObject PrepareQueryDTO dto) {
        dto.setUserId(UserHelp.userId());
        PageInfo<PrepareResultDTO> page = prepareService.page(dto, dto);
        return R.data(page);
    }

    /**
     * 获取当前用户备课区
     */
    @Operation(summary = "获取当前用户备课区")
    @GetMapping("/current")
    public R<List<PrepareResultDTO>> current() {
        List<PrepareResultDTO> page = prepareService.getCurrentUserPrepare();
        return R.data(page);
    }

    /**
     * 备课区-详情
     */
    @Operation(summary = "备课区-详情")
    @GetMapping("/{id}")
    public R<PrepareResultDTO> findById(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        PrepareResultDTO dto = prepareService.findById(id);
        return R.data(dto);
    }

    /**
     * 新增备课区
     */
    @Operation(summary = "新增备课区")
    @PostMapping
    public R<Long> insert(@RequestBody PrepareCreateDTO dto) {
        return R.data(prepareService.insert(dto), "创建成功");
    }

    /**
     * 新增默认备课区
     */
    @Operation(summary = "新增当前用户默认备课区")
    @PostMapping("/default")
    public R<Long> insertDefault() {
        return R.data(prepareService.insertDefault(Currents.userId()));
    }

    /**
     * 新增默认备课区
     */
    @Operation(summary = "新增默认备课区")
    @PostMapping("/{user-id}")
    public R<Long> insertDefault(@Parameter(description = "用户ID", example = "100001") @PathVariable("user-id") Long userId) {
        return R.data(prepareService.insertDefault(userId));
    }

    /**
     * 修改备课区
     */
    @Operation(summary = "修改备课区")
    @PutMapping("/{id}")
    public R<Boolean> update(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id, @RequestBody PrepareUpdateDTO dto) {
        dto.setId(id);
        return R.data(prepareService.updateById(dto));
    }

    /**
     * 删除备课区
     */
    @Operation(summary = "删除备课区")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        return R.data(prepareService.deleteById(id), "删除成功");
    }

}
